{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "## 额外的校验\n",
    "即使 q 是可选的，但只要提供了该参数，则该参数值不能超过50个字符的长度。"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:     Started server process [6784]\n",
      "INFO:     Waiting for application startup.\n",
      "INFO:     Application startup complete.\n",
      "INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:     127.0.0.1:57766 - \"GET /items HTTP/1.1\" 307 Temporary Redirect\n",
      "INFO:     127.0.0.1:57766 - \"GET /items/ HTTP/1.1\" 200 OK\n",
      "INFO:     127.0.0.1:57776 - \"GET /items?q=verygood HTTP/1.1\" 307 Temporary Redirect\n",
      "INFO:     127.0.0.1:57776 - \"GET /items/?q=verygood HTTP/1.1\" 200 OK\n",
      "INFO:     127.0.0.1:57783 - \"GET /items?q=verygoodhelloworld HTTP/1.1\" 307 Temporary Redirect\n",
      "INFO:     127.0.0.1:57783 - \"GET /items/?q=verygoodhelloworld HTTP/1.1\" 422 Unprocessable Entity\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:     Shutting down\n",
      "INFO:     Waiting for application shutdown.\n",
      "INFO:     Application shutdown complete.\n",
      "INFO:     Finished server process [6784]\n"
     ]
    }
   ],
   "source": [
    "from fastapi import FastAPI, Query\n",
    "from typing import Union\n",
    "import uvicorn\n",
    "\n",
    "app = FastAPI()\n",
    "\n",
    "\n",
    "@app.get(\"/items/\")\n",
    "async def read_item(q: Union[str, None] = Query(default=None, max_length=10)):\n",
    "    results = {\"items\": [{\"item_id\": \"Foo\"}, {\"item_id\": \"Bar\"}]}\n",
    "    if q:\n",
    "        results.update({\"q\": q})\n",
    "    return results\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    config = uvicorn.Config(app=app, host='127.0.0.1', port=8000)\n",
    "    server = uvicorn.Server(config)\n",
    "    await server.serve()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 显式声明必选参数\n",
    "将默认参数的默认值设为 ..."
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:     Started server process [6784]\n",
      "INFO:     Waiting for application startup.\n",
      "INFO:     Application startup complete.\n",
      "INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:     127.0.0.1:57855 - \"GET /items?q=verygood HTTP/1.1\" 307 Temporary Redirect\n",
      "INFO:     127.0.0.1:57855 - \"GET /items/?q=verygood HTTP/1.1\" 200 OK\n",
      "INFO:     127.0.0.1:57856 - \"GET /items HTTP/1.1\" 307 Temporary Redirect\n",
      "INFO:     127.0.0.1:57856 - \"GET /items/ HTTP/1.1\" 422 Unprocessable Entity\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:     Shutting down\n",
      "INFO:     Waiting for application shutdown.\n",
      "INFO:     Application shutdown complete.\n",
      "INFO:     Finished server process [6784]\n"
     ]
    }
   ],
   "source": [
    "from fastapi import FastAPI, Query\n",
    "from typing import Union\n",
    "import uvicorn\n",
    "\n",
    "app = FastAPI()\n",
    "\n",
    "\n",
    "@app.get(\"/items/\")\n",
    "async def read_item(q: Union[str, None] = Query(default=..., max_length=10)):\n",
    "    results = {\"items\": [{\"item_id\": \"Foo\"}, {\"item_id\": \"Bar\"}]}\n",
    "    if q:\n",
    "        results.update({\"q\": q})\n",
    "    return results\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    config = uvicorn.Config(app=app, host='127.0.0.1', port=8000)\n",
    "    server = uvicorn.Server(config)\n",
    "    await server.serve()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}